<!DOCTYPE html>
<html class="x-admin-sm">
{include file='public/header'}
<script src="/admin/xadmin/lib/layui/upload.js" charset="utf-8"></script>
<style>
    .layui-form-label{
        width: 120px;
    }
    .layui-input-block{
        margin-left: 150px;
    }
    #selectAccount .layui-input-block{
        margin-left: 0;
    }
</style>
<body>
    <div class="x-body">
        <form class="layui-form" lay-filter="formAjax">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>合作公司
                </label>
                <div id="pCompany" style="width:100%;flex:1"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>状态
                </label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="开启" checked>
                    <input type="radio" name="status" value="2" title="关闭">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>投放平台
                </label>
                <div id="pMenu" style="width:100%;flex:1"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    广告账户
                </label>
                <div style="display: flex;">
                    <div id="selectAccount" style="width:400px;flex:1"></div>
                    <button type="button" class="layui-btn" id="accountRefresh">刷新</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>广告账户ID
                </label>
                <div class="layui-input-block">
                    <input type="text" id="advertiser_id_input" name="account_id" autocomplete="off" class="layui-input" value="" />
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>返点
                </label>
                <div class="layui-input-block">
                    <input type="text" name="rebate" autocomplete="off" class="layui-input" value="" placeholder="请输入0~100之间的整数" />
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>账户主体
                </label>
                <div class="layui-input-block">
                    <input type="text" name="subject" autocomplete="off" class="layui-input" value="" placeholder="" />
                </div>
            </div>

            <input type="hidden" name="id" id="id" value="" />
            <button id="submitData" class="layui-btn" lay-submit lay-filter="submitData" style="display: none;" />

        </form>
    </div>
    <script>
        let params = {
            addUrl: '/cps/account/create',
            editUrl: '/cps/account/update',
            detailUrl: '/cps/account/detail',
        }
        form_ajax(params)

        let selectParams = {
            url: '/cps/link/getLaunchMedia',
            selected: [],
            tips: '选择平台',
            name: 'platform',
            field_id: 'id',
            field_Name: 'name',
            elem: '#pMenu',
            disabled:false
        }

        let selectAccount = {
            url: '/cps/link/accountSelectList',
            selected: [],
            tips: '选择广告账户',
            name: 'account_id',
            field_id: 'advertiser_id',
            field_Name: 'advertiser_name',
            elem: '#selectAccount'
        }

        let selectParams1 = {
            url: '/cps/account/companySelectList',
            selected: [],
            tips: '选择合作公司',
            name: 'company_id',
            field_id: 'id',
            field_Name: 'name',
            elem: '#pCompany'
        }

        let id = GetQueryString('id')

        function infoSelectN(option,value){
            if(option.elem === '#pCompany'){
                $('#config').show()
                selectParams1.url = `/cps/account/companySelectList`
                selectN(selectParams1)
            }
        }

        new Promise(function(reslove){
            if(id){
                Ajax(params.detailUrl, { id }).then(result => {
                    selectParams.disabled = true
                    selectParams.selected = [result.data.platform]
                    selectParams1.selected = [result.data.company_id]
                    selectAccount.selected = [result.data.account_id]
                    changeMediaUrl(result.data.media_id+'')
                    reslove()
                });
            }else{
                reslove()
            }
        }).then(function(){
            selectN(selectParams)
            selectN(selectParams1)
            selectN(selectAccount)
        })
        
        $('#accountRefresh').click(function(){
            selectAccount.url = selectAccount.url+'?is_refresh=1';
            selectAccount.loading = true;
            selectN(selectAccount)
        })

        $("#advertiser_id_input").on("input",function(e){
            //获取input输入的值
            selectAccount.selected = [e.delegateTarget.value]
            selectN(selectAccount)
        });
        function infoSelectN(option,value){
            if(option.elem == '#pMenu'){
                $("#advertiser_id_input").val('')
                changeMediaUrl(value)
            }
            if(option.elem == '#selectAccount'){
                $("#advertiser_id_input").val(value)
            }
        }
        function changeMediaUrl(value){
            switch (value) {
                case '1':
                selectAccount.url = '/cps/link/accountSelectList'
                    break;
                case '2':
                    selectAccount.url = '/cps/link/kwaiAccountSelectList'
                    break;
            }
            
            selectN(selectAccount)
        }
    </script>
</body>

</html>